#!/usr/bin/perl

package Swignition::Export::FBInfo;
use Swignition::Export;
use Swignition::Export::Contact;
use MIME::Base64;

sub is
{
	my $subject = shift;
	return unless ($subject);
	return unless (ref $subject eq 'Swignition::RDFModel::Subject');	
	return
		unless (defined $subject->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));
	foreach my $Type ( @{$subject
		->property('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
		->valObjects} )
	{
		my $type = $Type->toString;
		return 1
			if (($type eq 'http://www.w3.org/2002/12/cal/ical#Vfreebusy')
			||  ($type eq 'urn:ietf:rfc:2445#Vfreebusy'));
	}
	return;
}

sub outputs
{
	return [
		{
			code  => 'ICS',
			name  => 'iCalendar',
			type  => 'text/directory',
			multi => 1
		}
	];
}

sub outICS
{
	return "BEGIN:VCALENDAR\r\n"
		. _outICS(@_)
		. "END:VCALENDAR\r\n";
}

sub multiICS
{
	my $triples = shift;
	my $rv = '';
	foreach my $s (keys %$triples)
	{
		$rv .= _outICS($triples, $s)
			if (defined $triples->{$s} && is($triples->{$s}));
	}

	return "BEGIN:VCALENDAR\r\n"
		. $rv
		. "END:VCALENDAR\r\n";
}

sub _outICS
{
	my $triples = shift;
	my $subject = shift;
	my $S = findSubject($triples, $subject);
	my @rv = (['BEGIN','VFREEBUSY']);

	# UID
	my $uid = firstOf($S, 'urn:ietf:rfc:2445#uid',
		'http://www.w3.org/2002/12/cal/ical#uid',
		'http://purl.org/dc/terms/identifier',
		'http://purl.org/dc/elements/1.1/identifier');
	push @rv, ['UID', $uid]
		if ($uid);
	
	# Summary
	my $summary = firstOfLiteral($S,
		['urn:ietf:rfc:2445#summary',
		'http://www.w3.org/2002/12/cal/ical#summary',
		'http://purl.org/dc/terms/title',
		'http://purl.org/dc/elements/1.1/title'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['SUMMARY', $summary]
		if ($summary);
	
	# Freebusy
	my @fb = allOf($S, 'urn:ietf:rfc:2445#freebusy',
		'http://www.w3.org/2002/12/cal/ical#freebusy');
	foreach my $fb (@fb)
	{
		$fbSubject = findSubject($triples, $fb);
		if ($fbSubject)
		{
			my $fbtype = firstOfLiteral($fbSubject,
				['http://buzzword.org.uk/rdf/icalx#fbtype',
				'urn:ietf:rfc:2445#fbtype'],
				['urn:ietf:rfc:2445#value',
				'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
			$fbtype = 'BUSY' unless (length $fbtype);

			my $fbv;
			my @fbvals = allOfLiteral($fbSubject,
				['urn:ietf:rfc:2445#value',
				'http://www.w3.org/1999/02/22-rdf-syntax-ns#value'],
				['urn:ietf:rfc:2445#value',
				'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
			foreach my $fbv (@fbvals)
			{
				$fbval .= ',' . $fbv;
			}
			$fbval =~ s/^\,//;

			push @rv, ["FREEBUSY,FBTYPE=".uc($fbtype), $fbval, 1]
				if (length $fbval);
		}
		else
		{
			push @rv, ['FREEBUSY,VALUE=text', $fb];
		}
	}
	
	# Description
	my $desc = firstOfLiteral($S,
		['urn:ietf:rfc:2445#comment',
		'http://www.w3.org/2002/12/cal/ical#comment',
		'http://purl.org/dc/terms/description',
		'http://purl.org/dc/elements/1.1/description',
		'http://purl.org/dc/terms/abstract'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['COMMENT', $desc]
		if ($desc);
	
	# DTStart, DTEnd, DTStamp, Duration
	my $dt = firstOfLiteral($S, 
		['urn:ietf:rfc:2445#dtstart',
		'http://www.w3.org/2002/12/cal/ical#dtstart'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['DTSTART', Swignition::Export::CalComponent::_dt($dt)] if ($dt);
	$dt = firstOfLiteral($S, 
		['urn:ietf:rfc:2445#dtend',
		'http://www.w3.org/2002/12/cal/ical#dtend'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['DTEND', Swignition::Export::CalComponent::_dt($dt)] if ($dt);
	$dt = firstOfLiteral($S,
		['urn:ietf:rfc:2445#dtstamp',
		'http://www.w3.org/2002/12/cal/ical#dtstamp'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['DTSTAMP', Swignition::Export::CalComponent::_dt($dt)] if ($dt);
	$dt = firstOfLiteral($S, 
		['urn:ietf:rfc:2445#duration',
		'http://www.w3.org/2002/12/cal/ical#duration'],
		['urn:ietf:rfc:2445#value',
		'http://www.w3.org/1999/02/22-rdf-syntax-ns#value']);
	push @rv, ['DURATION', $dt] if ($dt);
	
	# URL
	my $url = firstOf($S, 'urn:ietf:rfc:2445#url',
		'http://www.w3.org/2002/12/cal/ical#url');
	push @rv, ['URL', $url]
		if ($url);
	
	# Attendees, Contact, Organizer
	push @rv, Swignition::Export::CalComponent::_people($triples, $S, 'aco');

	# Done.
	push @rv, ['END','VFREEBUSY'];
	
	# Convert to text/directory syntax.
	my $rv = '';
	foreach my $line (@rv)
	{
		my ($k, $v, $soft) = @{$line};
		
		if ($k =~ /VALUE.uri$/i && $v =~ /^data\:/)
		{
			my $uri = URI->new($v);
			$v = encode_base64($uri->data, '');
			$k =~ s/VALUE.uri$/ENCODING\=b/i;
			$k .= ';TYPE='.$uri->media_type
				if ($uri->media_type);
		}
		
		$v =~ s/\\/\\\\/g;
		$v =~ s/\r?\n/\\n/g;
		unless ($soft)
		{
			$v =~ s/\;/\\\;/g;
			$v =~ s/\,/\\\,/g;
		}
		
		$rv .= "$k:$v\r\n";
	}
	return $rv;
}

1;
